package logic

import (
	"context"
	"fmt"
	"tiktok/model/mysql"
	"tiktok/model/redis"

	"tiktok/rpc/internal/svc"
	"tiktok/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type TiktokLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTiktokLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TiktokLogic {
	return &TiktokLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TiktokLogic) Tiktok(in *pb.TiktokAddReq) (*pb.TiktokAddResp, error) {
	// todo: add your logic here and delete this line
	tiktok := mysql.Tiktok{
		Name:  in.Name,
		Phone: in.Phone,
	}
	err := tiktok.Add()
	if err != nil {
		logx.Error("信息有误")
		return nil, err
	}
	get := redis.Rdb.Get("send" + "register" + in.Phone)
	if get.Val() != in.Code {
		return nil, fmt.Errorf("短信验证有误")
	}
	time := redis.Rdb.Incr("send" + "register" + in.Phone)
	if time.Val() > 1 {
		return nil, fmt.Errorf("一分钟之内禁止再次发送验证码")
	}
	return &pb.TiktokAddResp{Name: tiktok.Name, Phone: tiktok.Phone}, nil
}
